<?php
/*
    OpenLib
    Copyright (C) 2011  Michael Gukov, Pham Thi Thuy Duong

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


namespace App\Models;

use Sun\Sun;
use Sun\Html;

class BriefList extends Html\Div {

    private $table;
    public function __construct(array $attr = array()) {
        parent::__construct($attr);
        $this->table = new Html\Table();
        $this->table->SetAttribute('class', 'BriefTable');
        $this->table->AddBody();

        $this->Add($this->table);
    }

    public function InsertAfterRow($after, $name, $title, $value = null, $hot = false, $veryhot = false) {
        $row = $this->table->First()
                ->InsertAfter($after, new Html\TRow(), $name)->Added();
        if ($hot) $row->SetAttribute('class' , 'Hot');
        if ($veryhot) $row->SetAttribute('class' , 'VeryHot');

        $row->AddData()->Added()->SetAttribute('class', 'Title')
            ->Add($title);

        $row->AddData()->Added()->SetAttribute('class', 'Value');
        if (!empty($value)) {
            $row->Last()->Add($value);
        }
        return $this;
    }

    public function AddRow($name, $title, $value = null, $hot = false, $veryhot = false) {
        $row = $this->table->First()->AddRow($name)->Added();
        if ($hot) $row->SetAttribute('class' , 'Hot');
        if ($veryhot) $row->SetAttribute('class' , 'VeryHot');

        $row->AddData()->Added()->SetAttribute('class', 'Title')
            ->Add($title);

        $row->AddData()->Added()->SetAttribute('class', 'Value');
        if (!empty($value)) {
            $row->Last()->Add($value);
        }
        return $this;
    }

    public function InsertValue($name, $value) {
        $this->table->First()->Get($name)->Last()->Add($value);
        return $this;
    }

    public function Contains($name) {
        return $this->table->First()->Contains($name);
    }
}
